package db;

/*
Cours :             LOG120 
Session :           Saison (hiver) 
Groupe :            01 
Projet :            Laboratoire #2
Etudiant(e)(s) :    Jeremy Moreau
                    Alexandre Richard
                    Philippe Pigeon
Code(s) perm. :     MORJ91010106
					RICA10028806 
                    PIGP08058903
Professeur :        Ghizlane El Boussaidi  
Date remise :       2012-03-010  
*/

import java.io.*;
import java.util.ArrayList;
import model.AbstractModele;
import model.Commande;

public class CarnetDeCommande extends AbstractModele 
{
	
	ArrayList<Commande> commandes = new ArrayList<Commande>();
	ArrayList<Commande> commandesReady = new ArrayList<Commande>();
	ArrayList<Commande> commandesDelivered = new ArrayList<Commande>();
	
	String COMMANDE_FILE = "commande.dat";
	String COMMANDE_READY_FILE = "commandeReady.dat";
	String COMMANDE_DELIV_FILE = "commandeDeliv.dat";
	
	boolean loaded = false;
	
	public void add(Commande _c) 
	{ 
		check();
		
		if(_c.isDelivered())
			commandesDelivered.add(_c);
		if(_c.isReady())
			commandesReady.add(_c);
		else
			commandes.add(_c); 
		setChanged();
		notifyObservers();
		
		save();
	}
	
	public ArrayList<Commande> getNonReadyCommandes()
	{
		check();
		return commandes;
	}
	
	public ArrayList<Commande> getReadyCommandes()
	{
		check();
		return commandesReady;
	}
	
	public ArrayList<Commande> getDeliveredCommandes()
	{
		check();
		return commandesDelivered;
	}
	
	
	
	public void setPrete(int id)
	{
		for(int i=0; i<commandesReady.size(); i++)
		{
			if(commandesReady.get(i).getIdConfirmation() == id)
			{
				commandes.get(i).setReady(true);
				commandesReady.add( commandesReady.get(i) );
				commandes.remove(i);
			}
		}
		setChanged();
		notifyObservers();
		save();
	}	
	
	public void setDelivered(int id)
	{
		for(int i=0; i<commandesReady.size(); i++)
		{
			if(commandesReady.get(i).getIdConfirmation() == id)
			{
				commandesReady.get(i).setDelivered(true);
				commandesDelivered.add( commandesReady.get(i) );
				commandesReady.remove(i);
			}
		}
		setChanged();
		notifyObservers();
		save();
	}
	
	public void save()
	{
		try
		{
			new File(COMMANDE_FILE).delete();
			new File(COMMANDE_READY_FILE).delete();
			new File(COMMANDE_DELIV_FILE).delete();
			
			ObjectOutputStream oos;
			FileOutputStream 
					fosC = new FileOutputStream(COMMANDE_FILE), 
					fosCR = new FileOutputStream(COMMANDE_READY_FILE), 
					fosCD = new FileOutputStream(COMMANDE_DELIV_FILE);
			
			oos = new ObjectOutputStream(fosC);
			oos.writeObject(commandes);
			
			oos = new ObjectOutputStream(fosCR);
			oos.writeObject(commandesReady);
			
			oos = new ObjectOutputStream(fosCD);
			oos.writeObject(commandesDelivered);
					
			fosC.close();
			fosCR.close();
			fosCD.close();
		} 
		catch (Exception e)
		{ e.printStackTrace(); }
	}
	
	public void load()
	{
		try
		{
			new File(COMMANDE_FILE).createNewFile();
			new File(COMMANDE_READY_FILE).createNewFile();
			new File(COMMANDE_DELIV_FILE).createNewFile();

			ObjectInputStream ois;
			FileInputStream 
					fisC = new FileInputStream(COMMANDE_FILE), 
					fisCR = new FileInputStream(COMMANDE_READY_FILE), 
					fisCD = new FileInputStream(COMMANDE_DELIV_FILE);
			
			ois = new ObjectInputStream(fisC);
			commandes = (ArrayList<Commande>)ois.readObject();
			
			ois = new ObjectInputStream(fisCR);
			commandesReady = (ArrayList<Commande>)ois.readObject();
			
			ois = new ObjectInputStream(fisCD);
			commandesDelivered = (ArrayList<Commande>)ois.readObject();
			
			fisC.close();
			fisCR.close();
			fisCD.close();

		}
        catch(EOFException eof) { System.out.println("Fichier vide, aucune informations chargé."); }
		catch(Exception e) { e.printStackTrace(); }
	}

	private void check()
	{
		if(!loaded)
		{
			load();
			loaded = true;
		}
	}
}

